window.freebase = {
  // Maps a specific Freebase relationship to a search filter used by the jQuery Freebase widget.
  getFilterByRelation: function(relation) {
    return {
      '/film/film/directed_by': '(all type:/film/director)',
      'contributor': '(all type:/film/actor)',
      '/film/film/genre': '(all type:/film/film_genre)',
      'certification': '(all type:/award/award_category)'
    }[relation];
  },

  // Given the user input via the jQuery Freebase widget, construct a search filter appropriate
  // for using with the Freebase Search API.
  // Freebase Search cookbook: https://developers.google.com/freebase/v1/search-cookbook
  buildFilter: function() {
    var filter = '(all type:/film/film';
    $('.search-pair').each(function() {
      var relation = $(this).find('select').val();
      var value = $(this).find('input[type=hidden]').val();
      filter += ' ' + relation + ':' + value;
    });

    return filter + ')';
  }
};